package com.ktjiaoyu.crm;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjiaoyu.crm.mapper.UserMapper;
import com.ktjiaoyu.crm.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
public class UserMapperTestr {
//    @Autowired
    @Resource
    private UserMapper userMapper;
    @Test
    public void testSelectById(){//主键查询
        User user=userMapper.selectById(1L);
        System.out.println("usrName: "+user.getUsrName());
    }
    @Test
    public void testInsert() {//添加
        userMapper.insert(new User("李荣华","123456",9L,1));
    }
    @Test
    public void testFindAll() {//查询所有
        List<User> list=userMapper.selectList(null);
        for (User user:list){
          System.out.println("usrName: "+user.getUsrName());
        }
    }
    @Test
    public void testFind(){//条件查询，根据用户名和密码查询
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("usr_name","小波老师");
        wrapper.eq("usr_password","123456");
        List<User> list=userMapper.selectList(wrapper);
        for (User user:list){
            System.out.println("usrId: "+user.getUsrId());
        }
    }
    @Test
    public void testPagination() {//分页查询
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.eq("usr_role_id",2L);//查询条件：角色Id为2
        //分页对象，查询获得User对象,查看第1页数据，每页显示2条记录
        Page<User> page=new Page<>(1,2);
        IPage<User> userPage=userMapper.selectPage(page,wrapper);
        System.out.println("总记录数："+userPage.getTotal());
        System.out.println("总页数："+userPage.getPages());
        System.out.println("当前页码："+userPage.getCurrent());
        System.out.println("记录"+userPage.getSize());
        System.out.println("-------");
        for (User user:userPage.getRecords()){
            System.out.println("usrId:"+user.getUsrId());
        }
    }
}
